package com.group.library.order.controller.admin;

import com.group.library.order.dto.MessageBean;
import com.group.library.order.entity.Admin;
import com.group.library.order.service.AdminBaseService;
import com.group.library.order.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/admin/base")
public class AdminBaseController {

    @Autowired
    private AdminBaseService adminBaseService;

    @RequestMapping(value = "/login.action",method = RequestMethod.POST)
    public MessageBean login(String name, String password, HttpSession session){
        try{
            Admin admin = adminBaseService.findAdmin(name, password);
            if(admin!=null){
                session.setAttribute("admin",admin);
                return MessageUtil.success();
            }
            return MessageUtil.failure("账号或密码错误！");
        }catch (Exception e){
            System.out.println("错误！");
            return MessageUtil.failure("错误异常！");
        }
    }

    @RequestMapping(value = "/logout.action",method = RequestMethod.GET)
    public MessageBean logout(HttpSession session){
        try{
            session.invalidate();
            return MessageUtil.success();
        }catch (Exception e){
            return MessageUtil.failure();
        }
    }


}
//package com.group.library.order.controller.admin;
//
//import com.group.library.order.dto.MessageBean;
//import com.group.library.order.entity.Admin;
//import com.group.library.order.service.AdminBaseService;
//import com.group.library.order.util.MessageUtil;
//import lombok.val;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpSession;
//
//@RestController
//@RequestMapping("/admin/base")
//public class AdminBaseController {
//
//    @Autowired
//    private AdminBaseService adminBaseService;
//
//    @RequestMapping(value = "/login.action",method = RequestMethod.POST)
//    public MessageBean login(String name, String password, HttpSession session){
//        try{
//            Admin admin = adminBaseService.findAdmin(name, password);
//            if(admin!=null){
//                session.setAttribute("admin",admin);
//                return MessageUtil.success();
//            }
//            return MessageUtil.failure("账号或密码错误！");
//        }catch (Exception e){
//            System.out.println("错误！");
//            return MessageUtil.failure("错误异常！");
//        }
//    }
//
//    @RequestMapping(value = "/logout.action",method = RequestMethod.GET)
//    public MessageBean logout(HttpSession session){
//        try{
////            session.removeAttribute("admin");
//            session.invalidate();
//            return MessageUtil.success();
//        }catch (Exception e){
//            return MessageUtil.failure();
//        }
//    }
//
//
//}
